/**
 * 
 */
package com.aem.sticky.button;

import com.aem.sticky.button.events.ButtonListener;
import com.aem.sticky.button.events.ClickListener;
import com.aem.sticky.button.events.NullListener;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;


/**
 * Button with text instead of an image.
 * 
 * @author abacs
 *
 */
public class TextButton extends ButtonSkeleton implements ButtonListener,
		ClickListener {

	private ClickListener clickListener;
	private ButtonListener buttonListener;
	
	protected Shape shape;
	private Sound click;
	
	protected String text;
	private boolean mouseOver = false;
	
	/** Text padding in pixels */
	protected int padding = 2;
	
	/** left corner */
	protected float x = 0;
	/**  top corner */
	protected float y = 0;
	
	private Color colorMouseOver = Color.red;
	
	private Color color = Color.white;
	
	// -------------------------------------------------------------------------
	// constructors:
	
	/** 
	 * 
	 * 
	 */
	public TextButton(float leftX, float topY, String text, Sound click) {
		this.text = text;
		
		this.x = leftX;
		this.y = topY;
		
		this.shape = new Rectangle(-1,-1,0,0);
		this.setShape(this.shape);
		
		this.click = click;
		clickListener = NullListener.getSingleton();
		buttonListener = NullListener.getSingleton();
		super.addListener((ButtonListener) this);
		super.addListener((ClickListener) this);
	}
	
	
	// -------------------------------------------------------------------------
	
	
	@Override
	public void addListener(ClickListener l) {
		clickListener = l;
	}
	
	@Override
	public void addListener(ButtonListener b) {
		buttonListener = b;
	}
	
	@Override
	public void render(GameContainer container, Graphics graphics) {
		
		if(this.shape.getWidth() == 0){
			Font font = graphics.getFont();
			int width = font.getWidth(text) + this.padding * 2;
			int height = font.getHeight(text) + this.padding * 2;
			this.shape = new Rectangle(this.x, this.y, width, height);
			this.setShape(this.shape);
		}
		
		if(this.mouseOver){
			graphics.setColor(this.colorMouseOver);
			graphics.drawRect(
					this.shape.getX(),
					this.shape.getY(),
					this.shape.getWidth(),
					this.shape.getHeight()
			);
		}else{
			graphics.setColor(this.color);
		}
		
		// draw it:
		graphics.drawString(
				this.text,
				this.shape.getX() + this.padding,
				this.shape.getY() + this.padding
		);
	}
	
	@Override
	public void onClick(Button clicked, float mx, float my) {
		if(clickListener != null)
			clickListener.onClick(this, mx, my);
		click.play();
	}
	
	@Override
	public void onDoubleClick(Button clicked, float mx, float my) {
		if(clickListener != null)
			clickListener.onDoubleClick(this, mx, my);
		click.play();
	}
	
	@Override
	public void onRightClick(Button clicked, float mx, float my) {
		if(clickListener != null)
			clickListener.onRightClick(this, mx, my);
		click.play();
	}
	
	@Override
	public void onMouseEnter(Button b) {
		buttonListener.onMouseEnter(this);
		this.mouseOver = true;
	}
	
	@Override
	public void onMouseExit(Button b) {
		buttonListener.onMouseExit(this);
		this.mouseOver = false;
	}
	
	// -------------------------------------------------------------------------
	
	public void setColor(Color color) {
		this.color = color;
	}
	
	public void setColorMouseOver(Color color) {
		this.colorMouseOver = color;
	}
	
	public void setPadding(int padding) {
		this.padding = padding;
	}
	
	public void setText(String text) {
		this.text = text;
		this.shape = new Rectangle(this.x, this.y, 0, 0);
	}
	
	/**
	 * 
	 * @param x corner position x
	 * @param y corner position y
	 */
	public void moveTo(float x, float y) {
		this.x = x;
		this.y = y;
		this.shape = new Rectangle(x, y, 0, 0);
	}

	/**
	 * 
	 */
	public void hide() {
		this.moveTo(-this.shape.getWidth(), -this.shape.getHeight());
		
	}
	
	public void removeClickListener() {
		this.clickListener = null;
	}
	
	// -------------------------------------------------------------------------
	
	public Shape getShape() {
		return this.shape;
	}
	
	public String getText() {
		return this.text;
	}
	
	// -------------------------------------------------------------------------
	
	
	
}
